package com.up.upzl.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.up.upzl.base.service.impl.BaseServiceImpl;
import com.up.upzl.biz.domain.Article;
import com.up.upzl.biz.domain.Article.AddSource;
import com.up.upzl.biz.domain.Article.Auditing;
import com.up.upzl.biz.domain.Module.PageType;
import com.up.upzl.biz.domain.Module.ShowType;
import com.up.upzl.biz.domain.ModuleArticle;
import com.up.upzl.biz.repository.ArticleRepository;
import com.up.upzl.biz.service.ArticleService;

import utils.UpzlUtils;

@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article, Long> implements ArticleService {

	@Autowired
	private ArticleRepository articleRepository;

	@Override
	public Page<Article> findByCondition(Long oneCatId, Long catId, AddSource addSource, Auditing auditing,
			String title, Date beginTime, Date endTime, Pageable page) {
		return articleRepository.findAll(new Specification<Article>() {

			@Override
			public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (!StringUtils.isEmpty(oneCatId)) {
					predicates.add(cb.and(cb.equal(root.join("oneArticleCategory").get("id"), oneCatId)));
				}
				if (!StringUtils.isEmpty(catId)) {
					predicates.add(cb.and(cb.equal(root.join("articleCategory").get("id"), catId)));
				}
				if (!StringUtils.isEmpty(title)) {
					predicates.add(cb.and(cb.like(cb.lower(root.get("title")), "%" + title.toLowerCase() + "%")));
				}

				if (!StringUtils.isEmpty(addSource)) {
					predicates.add(cb.and(cb.equal(root.get("addSource"), addSource)));
				}
				if (!StringUtils.isEmpty(auditing)) {
					predicates.add(cb.and(cb.equal(root.get("auditing"), auditing)));
				}
				if (beginTime != null) {
					predicates.add(cb.and(cb.greaterThanOrEqualTo(root.get("createTime"), beginTime)));
				}
				if (endTime != null) {
					predicates.add(cb.and(cb.lessThanOrEqualTo(root.get("createTime"), UpzlUtils.timeHelper(endTime))));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		}, page);
	}

	@Override
	public Integer setToTop(Long id) {
		return articleRepository.setToTop(id);
	}

	@Override
	public List<ModuleArticle> findByModuleType(PageType pageType, ShowType showType) {
		return articleRepository.findByModuleType(pageType, showType);
	}

	@Override
	public Boolean idExists(Long id) {
		return articleRepository.existsById(id);
	}

}
